package com.example.shixun.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.shixun.pojo.Admin;
import com.example.shixun.pojo.AdminDTO;
import com.example.shixun.pojo.Result;
import com.example.shixun.service.AdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @ApiOperation("admin登录")
    @PostMapping("/login")
    public Result<Integer> index(@RequestBody Admin admin, HttpServletRequest request){
        //public Result<Integer> index(String email,String password, HttpServletRequest request){
        System.out.println("访问登录界面 adminMsg="+admin.toString());
        //System.out.println(email+":"+password);
        if(admin.getEmail()==null||admin.getPassword()==null){
            return Result.fail("邮箱或密码为空！");
        }
//        if(!adminService.login(admin.getEmail()).getSuccess()){
//            return Result.fail("该邮箱未注册");
//        }
        admin.setEmail(admin.getEmail());
        admin.setPassword(admin.getPassword());
        Result r = adminService.login(admin);
        if(r.getSuccess()){
            HttpSession session= request.getSession();
            session.setAttribute("user",admin);
        }
        return r;
    }
    @ApiOperation("根据token获取管理员信息")
    @GetMapping("info")
    public Result info(@RequestHeader("token") String token){
        if (token == null) return Result.fail("token==null");
        AdminDTO adminDTO = adminService.getAdminTokenInfo(token);
        System.out.println("adminDTO="+adminDTO);
        String jsonStr = JSON.toJSONString(adminDTO, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
        System.out.println("map="+map);
        return Result.success(map);
    }
    @ApiOperation("根据token里的id获取用户信息")
    @GetMapping("infoById")
    public Result infoById(@RequestHeader("token") String token) {
//        System.out.println("token="+token);
        if (token == null) return Result.fail("token==null");
        AdminDTO adminDTO = adminService.getAdminTokenInfo(token);
        System.out.println("adminDTO="+adminDTO);
        String jsonStr = JSON.toJSONString(adminDTO, SerializerFeature.WriteDateUseDateFormat);
        Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {});
//        System.out.println("map="+map);
        return Result.success(adminService.getAdminById((Integer) map.get("id")));
    }
}
